package com.u17;

/**
 * @author u17
 * https://leetcode-cn.com/problems/merge-sorted-array/
 */
public class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = 0;
        int j = 0;
        int s = 0;
        int[] result = new int[m];
        for (int q = 0; q < m; q++) {
            result[q] = nums1[q];
        }
        while (i < m || j < n) {
            if (i == m) {
                nums1[s] = nums2[j++];
            } else if (j == n) {
                nums1[s] = result[i++];
            } else if (result[i] < nums2[j]) {
                nums1[s] = result[i++];
            } else {
                nums1[s] = nums2[j++];
            }

            s++;
        }

        for (int num: nums1) {
            System.out.print(num);
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] num1 = new int[]{1, 2, 3, 0, 0, 0};
        int m = 3;
        int[] num2 = new int[]{2, 5, 6};
        int n = 3;
        solution.merge(num1, m, num2, n);
    }
}
